package com.freesky.biz.psbc.service.impl;

import com.freesky.biz.icbc.domain.TIcbcHappyIot;
import com.freesky.biz.icbc.service.IIcbcHappyIotService;
import com.freesky.biz.psbc.domain.TPsbcCoupon;
import com.freesky.biz.psbc.domain.TPsbcStock;
import com.freesky.biz.psbc.mapper.PsbcCouponMapper;
import com.freesky.biz.psbc.mapper.PsbcStockMapper;
import com.freesky.biz.psbc.service.IPsbcCouponService;
import com.freesky.biz.psbc.service.IPsbcStockService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PsbcCouponServiceImpl implements IPsbcCouponService {

    @Autowired
    private PsbcCouponMapper couponMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;


    @Override
    public List<TPsbcCoupon> findList(TPsbcCoupon entity) {
        return couponMapper.findList(entity);
    }


    @Override
    public AjaxResult exportFile(TPsbcCoupon entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("PSBC_THIRD_CPN");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TPsbcCoupon(), IPsbcCouponService.class,
                "findList", "邮储发券",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public String getPsbcCardByReceiveId(String receiveId) {
        return couponMapper.getPsbcCardByReceiveId(receiveId);
    }

    @Override
    public TPsbcCoupon selectPsbcCouponById(String id) {
        return couponMapper.selectPsbcCouponById(id);
    }

}
